/*
 * AnchorControl.java
 *
 * Created on 24 November 2004, 09:48
 * Copyright (C) 2004, Velocityme Partnership
 */

package com.velocityme.client.gui.node.task.anchor;

import com.velocityme.client.gui.ChangeDeltaPanel;
import com.velocityme.client.gui.GuiDisplayChangeState;
import com.velocityme.client.gui.exception.ExceptionWindow;
import com.velocityme.client.gui.node.NodePostComment;
import com.velocityme.client.gui.time.TimePanel;
import com.velocityme.client.utility.StatelessRemoteClientSessionUtil;
import com.velocityme.entity.PermissionBean;
import com.velocityme.interfaces.AnchorPK;
import com.velocityme.interfaces.ContactablePK;
import com.velocityme.interfaces.KeySession;
import com.velocityme.interfaces.NodePK;
import com.velocityme.interfaces.NodeUtil;
import com.velocityme.interfaces.PermissionPK;
import com.velocityme.interfaces.TaskPK;
import com.velocityme.interfaces.TaskUtil;
import com.velocityme.utility.PermissionDeniedException;
import com.velocityme.utility.InvalidKeyException;
import com.velocityme.valueobjects.AnchorValue;
import com.velocityme.valueobjects.ContactableValue;
import java.rmi.RemoteException;
import java.util.Collection;
import java.util.Set;
import javax.ejb.CreateException;
import javax.ejb.FinderException;
import javax.naming.NamingException;
import javax.swing.JOptionPane;
import javax.swing.JTabbedPane;
import javax.swing.tree.TreeModel;

/**
 *
 * @author  Robert Crida Work
 */
public class AnchorControl extends javax.swing.JPanel {
    
    private AnchorPK m_anchorPK;
    private NodePK m_parentNodePK;
    private KeySession m_key;
    private AnchorValueDisplay m_anchorValueDisplay;
    private boolean m_editing = true;
    private Set m_setPermissions;
    private ContactableValue m_defaultOwnerValue;
    private Collection m_interestedContactableValues;
    private Collection m_responsibleContactableValues;
    private TreeModel m_allContactableValues;
    private boolean m_displayResponsibility = false;

    /** Creates new form AnchorControl */
    public AnchorControl(KeySession p_key, AnchorPK p_anchorPK, boolean p_editing, Set p_setPermissions) {
        initComponents();
        m_key = p_key;
        m_anchorPK = p_anchorPK;
        m_editing = p_editing;
        m_setPermissions = p_setPermissions;
        try {
            m_displayResponsibility = true;
            if (p_editing) {
                m_allContactableValues = StatelessRemoteClientSessionUtil.getSession().getContactableValueTree(m_key);
            }
            AnchorValue anchorValue = StatelessRemoteClientSessionUtil.getSession().getAnchorValue(m_key, m_anchorPK);
            m_defaultOwnerValue = anchorValue.getTaskValue().getOwnerContactableValue();
            m_interestedContactableValues = StatelessRemoteClientSessionUtil.getSession().getTaskInterestedContactableValues(m_key, new TaskPK(m_anchorPK.getAnchorId()));
            m_responsibleContactableValues = StatelessRemoteClientSessionUtil.getSession().getTaskResponsibleContactableValues(m_key, new TaskPK(m_anchorPK.getAnchorId()));
            m_anchorValueDisplay = new AnchorValueDisplay(anchorValue, m_defaultOwnerValue, m_interestedContactableValues, m_responsibleContactableValues, m_allContactableValues, m_displayResponsibility);
            if (!p_editing) {
                jTabbedPane1.addTab("Change Log", new ChangeDeltaPanel(m_key, new NodePK(m_anchorPK.getAnchorId())));
                jTabbedPane1.addTab("Time", new TimePanel(m_key, new TaskPK(m_anchorPK.getAnchorId()), m_setPermissions));
                jButtonEdit.setEnabled(m_setPermissions.contains(new PermissionPK(new Integer(PermissionBean.ACTION_EDIT))));
                jButtonComment.setEnabled(m_setPermissions.contains(new PermissionPK(new Integer(PermissionBean.NODE_POST_COMMENT))));
                jButtonInterest.setEnabled(true);
            }
            else {
                jButtonOK.setEnabled(true);
                jButtonCancel.setEnabled(true);
            }
            m_anchorValueDisplay.setEditable(p_editing);
            jPanel3.add(m_anchorValueDisplay, java.awt.BorderLayout.CENTER);
        }
        catch(RemoteException e) {
            ExceptionWindow.showError(e);
        }
        catch(NamingException e) {
            ExceptionWindow.showError(e);
        }
        catch(CreateException e) {
            ExceptionWindow.showError(e);
        }
        catch(InvalidKeyException e) {
            ExceptionWindow.showError(e);
        }
        catch(PermissionDeniedException e) {
            JOptionPane.showMessageDialog(this, "Permission denied!", "View Anchor", JOptionPane.WARNING_MESSAGE);
        }
    }
    
    /** Creates new form AnchorControl */
    public AnchorControl(KeySession p_key, NodePK p_parentNodePK) {
        initComponents();
        m_key = p_key;
        m_parentNodePK = p_parentNodePK;
        try {
            m_defaultOwnerValue = StatelessRemoteClientSessionUtil.getSession().getUserPersonValue(m_key, m_key.getUserPK()).getContactableValue();
            m_allContactableValues = StatelessRemoteClientSessionUtil.getSession().getContactableValueTree(m_key);
            m_anchorValueDisplay = new AnchorValueDisplay(new AnchorValue(), m_defaultOwnerValue, null, null, m_allContactableValues, m_displayResponsibility);
            m_anchorValueDisplay.setEditable(true);
            jPanel3.add(m_anchorValueDisplay, java.awt.BorderLayout.CENTER);
        }
        catch(RemoteException e) {
            ExceptionWindow.showError(e);
        }
        catch(NamingException e) {
            ExceptionWindow.showError(e);
        }
        catch(CreateException e) {
            ExceptionWindow.showError(e);
        }
        catch(InvalidKeyException e) {
            ExceptionWindow.showError(e);
        }
        jButtonOK.setEnabled(true);
        jButtonCancel.setEnabled(true);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel3 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jButtonEdit = new javax.swing.JButton();
        jButtonOK = new javax.swing.JButton();
        jButtonCancel = new javax.swing.JButton();
        jButtonComment = new javax.swing.JButton();
        jButtonInterest = new javax.swing.JButton();

        setLayout(new java.awt.BorderLayout());

        jPanel3.setLayout(new java.awt.BorderLayout());

        jPanel3.addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                jPanel3ComponentShown(evt);
            }
        });

        jPanel2.setLayout(new java.awt.GridLayout(1, 0));

        jButtonEdit.setText("Edit");
        jButtonEdit.setEnabled(false);
        jButtonEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonEditActionPerformed(evt);
            }
        });

        jPanel2.add(jButtonEdit);

        jButtonOK.setText("OK");
        jButtonOK.setEnabled(false);
        jButtonOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonOKActionPerformed(evt);
            }
        });

        jPanel2.add(jButtonOK);

        jButtonCancel.setText("Cancel");
        jButtonCancel.setEnabled(false);
        jButtonCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCancelActionPerformed(evt);
            }
        });

        jPanel2.add(jButtonCancel);

        jButtonComment.setText("Post Comment");
        jButtonComment.setEnabled(false);
        jButtonComment.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCommentActionPerformed(evt);
            }
        });

        jPanel2.add(jButtonComment);

        jButtonInterest.setText("Toggle Interest");
        jButtonInterest.setEnabled(false);
        jButtonInterest.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonInterestActionPerformed(evt);
            }
        });

        jPanel2.add(jButtonInterest);

        jPanel3.add(jPanel2, java.awt.BorderLayout.SOUTH);

        jTabbedPane1.addTab("Anchor", jPanel3);

        add(jTabbedPane1, java.awt.BorderLayout.CENTER);

    }//GEN-END:initComponents

    private void jButtonInterestActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonInterestActionPerformed
        // TODO add your handling code here:
        try {
            StatelessRemoteClientSessionUtil.getSession().taskToggleInterestedContactableStateValue(m_key, new TaskPK(m_anchorPK.getAnchorId()));
            m_interestedContactableValues = StatelessRemoteClientSessionUtil.getSession().getTaskInterestedContactableValues(m_key, new TaskPK(m_anchorPK.getAnchorId()));
            m_anchorValueDisplay.setAnchorValue(StatelessRemoteClientSessionUtil.getSession().getAnchorValue(m_key, m_anchorPK), m_defaultOwnerValue, m_interestedContactableValues, m_responsibleContactableValues, m_allContactableValues, m_displayResponsibility);
        }
        catch(RemoteException e) {
            ExceptionWindow.showError(e);
        }
        catch(NamingException e) {
            ExceptionWindow.showError(e);
        }
        catch(CreateException e) {
            ExceptionWindow.showError(e);
        }
        catch(InvalidKeyException e) {
            ExceptionWindow.showError(e);
        }
        catch(PermissionDeniedException e) {
            JOptionPane.showMessageDialog(this, "Permission denied!", "View Anchor", JOptionPane.WARNING_MESSAGE);
        }        
    }//GEN-LAST:event_jButtonInterestActionPerformed

    private void jButtonCommentActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCommentActionPerformed
        // TODO add your handling code here:
        NodePostComment commentPanel = new NodePostComment (m_key, new NodePK(m_anchorPK.getAnchorId()));
        ((JTabbedPane)getParent()).addTab("Post Comment", commentPanel);
        ((JTabbedPane)getParent()).setSelectedComponent(commentPanel);  
    }//GEN-LAST:event_jButtonCommentActionPerformed

    private void jButtonCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCancelActionPerformed
        // Add your handling code here:
        getParent().remove(this);       
    }//GEN-LAST:event_jButtonCancelActionPerformed

    private void jButtonOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonOKActionPerformed
        // Add your handling code here:
        try {
            AnchorValue anchorValue = m_anchorValueDisplay.getAnchorValue();
            if (anchorValue.anchorIdHasBeenSet()) {
                ContactablePK[] interestedContactablePKs = m_anchorValueDisplay.getInterestedContactablePKs();
                ContactablePK[] responsibleContactablePKs = m_anchorValueDisplay.getResponsibleContactablePKs();
                StatelessRemoteClientSessionUtil.getSession().anchorEdit(m_key, m_anchorPK, anchorValue, responsibleContactablePKs, interestedContactablePKs);
            }
            else {
                AnchorPK anchorPK = StatelessRemoteClientSessionUtil.getSession().anchorCreate(m_key, m_parentNodePK, anchorValue);
                if (m_anchorValueDisplay.getInterestedDefault())
                    StatelessRemoteClientSessionUtil.getSession().taskEnableInterestedContactableStateValue(m_key, new TaskPK(anchorPK.getAnchorId()));
            }
            getParent().remove(this);
        }
        catch(RemoteException e) {
            ExceptionWindow.showError(e);
        }
        catch(NamingException e) {
            ExceptionWindow.showError(e);
        }
        catch(CreateException e) {
            ExceptionWindow.showError(e);
        }
        catch(PermissionDeniedException e) {
            ExceptionWindow.showError(e);
        }
        catch(InvalidKeyException e) {
            ExceptionWindow.showError(e);
        }        
    }//GEN-LAST:event_jButtonOKActionPerformed

    private void jButtonEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonEditActionPerformed
        // Add your handling code here:
        AnchorControl editPanel = new AnchorControl(m_key, m_anchorPK, true, m_setPermissions);
        ((JTabbedPane)getParent()).addTab("Edit Anchor", editPanel);
        ((JTabbedPane)getParent()).setSelectedComponent(editPanel);        
    }//GEN-LAST:event_jButtonEditActionPerformed

    private void jPanel3ComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_jPanel3ComponentShown
        // Add your handling code here:
        try {
            if (!m_editing)
                m_anchorValueDisplay.setAnchorValue(StatelessRemoteClientSessionUtil.getSession().getAnchorValue(m_key, m_anchorPK), m_defaultOwnerValue, m_interestedContactableValues, m_responsibleContactableValues, m_allContactableValues, m_displayResponsibility);
        }
        catch(CreateException e) {
             ExceptionWindow.showError(e);
        }        
        catch(InvalidKeyException e) {
             ExceptionWindow.showError(e);
        }        
        catch(NamingException e) {
             ExceptionWindow.showError(e);
        }        
        catch(RemoteException e) {
             ExceptionWindow.showError(e);
        }
        catch(PermissionDeniedException e) {
            JOptionPane.showMessageDialog(this, "Permission denied!", "View Anchor", JOptionPane.WARNING_MESSAGE);
        }        
    }//GEN-LAST:event_jPanel3ComponentShown
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonCancel;
    private javax.swing.JButton jButtonComment;
    private javax.swing.JButton jButtonEdit;
    private javax.swing.JButton jButtonInterest;
    private javax.swing.JButton jButtonOK;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JTabbedPane jTabbedPane1;
    // End of variables declaration//GEN-END:variables
    
}
